﻿using FluentValidation;
using IdentityWebApi;
using Microsoft.EntityFrameworkCore;

internal class UserValidator:AbstractValidator<User>
{
    public UserValidator()
    {
        RuleFor(x => x.UserName).NotEmpty().WithMessage("用户名不能为空");
        RuleFor(x => x.Email)
            .EmailAddress()
            .Must(v=>v.EndsWith("@qq.com")||v.EndsWith("@163.com")).WithMessage("只支持QQ和163邮箱")
            .NotEmpty().WithMessage("邮箱不能为空");
        RuleFor(x => x.PhoneNumber).NotEmpty().WithMessage("手机号不能为空");
        RuleFor(x => x.NickName).NotEmpty().WithMessage("昵称不能为空");
        RuleFor(x => x.CreateTime).NotEmpty().WithMessage("创建时间不能为空");
    }
}

public record Login3Request(string UserName, string Password);

public class Login3RequestValidator : AbstractValidator<Login3Request>
{
    public Login3RequestValidator(MySqlDbContext mySqlDbContext)
    {
        RuleFor(x => x.UserName).NotEmpty().WithMessage("用户名不能为空")
            .MustAsync((name,_)=>mySqlDbContext.Users.AnyAsync(u=>u.UserName== name)).WithMessage(c=>$"用户名：{c.UserName}不存在");
        RuleFor(x => x.Password).NotEmpty().WithMessage("密码不能为空");
    }
}